<?php
App::uses('AppController', 'Controller');
/**
 * Products Controller
 *
 * @property Product $Product
 * @property PaginatorComponent $Paginator
 */
class ProductsController extends AppController {
	
	var $uses = array('Product', 'ProductIntro', 'ProductCategory');	
/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
    public $paginate = array(
        'limit' => 8,
    );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		//$options = array('conditions' => array('ProductIntro.' . $this->ProductIntro->primaryKey => 5));
		//$this->set('productIntro', $this->ProductIntro->find('first', $options));
		
		$this->set('productCategory', $this->ProductCategory->find('all'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->ProductIntro->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		$options = array('conditions' => array('ProductIntro.' . $this->ProductIntro->primaryKey => $id));
		$productIntro = $this->ProductIntro->find('first', $options);
		$this->set('productIntro', $productIntro);
		
		$folder = str_replace(' ', '_', strtolower($productIntro['ProductCategory']['name_eng']));
		$dir = WWW_ROOT . 'files/images/'. $folder;		
		// Open a directory, and read its contents
		$imageList = array();
		if (is_dir($dir)){
			if ($dh = opendir($dir)){
				while (($file = readdir($dh)) !== false){
					if(strlen($file) > 2) {
						$imageList[] = $file;
					}
				}
				closedir($dh);
			}
		}	

		$this->set('productCategory', $this->ProductCategory->find('all'));
		$this->set('imageList', $imageList);
		$this->set('folder', $folder);
	}
	
	/**
 * detail method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function detail($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
		$this->set('product', $this->Product->find('first', $options));
		
		$this->set('productCategory', $this->ProductCategory->find('all'));
	}
	
	public function lists() {
        $this->Product->recursive = -1;
        $this->Paginator->settings = $this->paginate;

		$this->set('products', $this->Paginator->paginate('Product'));
		$productCategory = $this->ProductCategory->find('all');
		$this->set('categoryName', end($productCategory));
		$this->set('productCategory', $productCategory);
        $this->set('article', $this->ProductIntro->findById(6));
	}
	
	public function search() {
		$keyword = $this->request->query('keyword');
		$this->Product->recursive = -1;
		$this->paginate = array(
			'conditions' => array(
						'OR' => array(
							'name LIKE' => '%' . $keyword . '%',
							'code LIKE' => '%' . $keyword . '%'
						)
					),
			'limit' => 8,
			'order' => array(
				'Product.created_date' => 'DESC'
			)
		);
        $this->Paginator->settings = $this->paginate;
		
		$this->set('keyword', $keyword);
		$this->set('products', $this->Paginator->paginate('Product'));
		$this->set('productCategory', $this->ProductCategory->find('all'));
	}
}
